package com.ruoyi.project.video.utils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

/**
 * 描述: Url2Base64Utils
 * 创建时间: 2023/3/8 00:51
 * 作者: guozy
 * 版本: 1.0
 */
public class Url2Base64Utils {
    /**
     * 根据文件url获取文件并转换为base64编码
     *
     * @param client
     * @param builder
     * @param srcUrl  文件地址
     * @return 文件base64编码
     */
    public static String netSourceToBase64(OkHttpClient client, Request.Builder builder, String srcUrl) throws IOException {
        Request request = builder.url(srcUrl).addHeader("User-Agent", "/android/i").build();
        Response response = client.newCall(request).execute();
        // 对字节数组Base64编码
        return "data:image/jpg;base64," + Base64.getEncoder().encodeToString(response.body().bytes()).replace("\n", "");
    }
}
